﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VirtualOrg.DB;
using VirtualOrg.Helper;

namespace VirtualOrg.Web
{
    public partial class FormLoginUser : OboutInc.oboutAJAXPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string Login(string username, string password)
        {
            MessageBox msgbox = null;
            string result = "";
            bool validLogin = false;
            try
            {
                using (DbStoreDataContext dataContext = new DbStoreDataContext())
                {
                    var loginResult = dataContext.CheckUserLogin(username, password);
                    SessionInfo sessionInfo = new SessionInfo();
                    foreach (var item in loginResult)
                    {
                        sessionInfo.UserID = item.UserID;
                        sessionInfo.UserName = item.Username;
                        sessionInfo.FullName = item.FullName;
                        sessionInfo.ImagePath = item.ProfilePic;
                        validLogin = true;
                    }

                    if (!validLogin)
                    {
                        msgbox = new MessageBox("Invalid username and password!!");
                        result = msgbox.ReturnObject();
                    }
                    else
                    {
                        Response.Cookies.Add(new HttpCookie(SiteConstants.CookieName, SiteHelper.EncryptSession(sessionInfo)));
                        Response.Cookies[SiteConstants.CookieName].Expires = DateTime.Now.AddHours(1);
                    }
                }
            }
            catch
            {
                msgbox = new MessageBox("Error while login!!");
                result = msgbox.ReturnObject();
            }
            
            return result;
        }
    }
}